﻿using HZY.Framework.DependencyInjection;
using Long.BLL;
using Long.Helper;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Long.SpraySCADA
{
    public partial class PageAuthManage : UIPage, ISingletonSelfDependency
    {
        private readonly AuthManager _authManager;
        public PageAuthManage(AuthManager authManager)
        {
            InitializeComponent();
            _authManager = authManager;
        }

        private async void PageAuthManage_Load(object sender, EventArgs e)
        {
            await LoadAuthData(SystemEnums.UserRole.工程师, cbg_Engineer);
            await LoadAuthData(SystemEnums.UserRole.操作员, cbg_Operator);
            await LoadAuthData(SystemEnums.UserRole.访客, cbg_Visitor);
        }

        /// <summary>
        /// 加载角色权限
        /// </summary>
        /// <param name="role"></param>
        /// <param name="cbg"></param>
        /// <returns></returns>
        private async Task LoadAuthData(SystemEnums.UserRole role, UICheckBoxGroup cbg)
        {
            BaseResult<AuthRequestDto>? res = await _authManager.GetAuthAsync(new AuthQueryDto() { Role = role.ToString() });
            if (res.ResultStatus == SystemEnums.ResultStatus.Success)
            {
                var auth = res.Data[0];
                List<int> ints = new List<int>();
                if (auth.ControlModule)
                {
                    ints.Add(0);
                }
                if (auth.MonitorModule)
                {
                    ints.Add(1);
                }
                if (auth.RecipeModule)
                {
                    ints.Add(2);
                }
                if (auth.LogModule)
                {
                    ints.Add(3);
                }
                if (auth.ReportModule)
                {
                    ints.Add(4);
                }
                if (auth.ChartModule)
                {
                    ints.Add(5);
                }
                if (auth.ParamModule)
                {
                    ints.Add(6);
                }
                cbg.SelectedIndexes = ints;
            }

        }

        private async void btn_UpdateEngAuth_Click(object sender, EventArgs e)
        {
          await  UpdateAuth(SystemEnums.UserRole.工程师, 1, cbg_Engineer);
        }

        private  async void btn_UpdateOpAuth_Click(object sender, EventArgs e)
        {
            await UpdateAuth(SystemEnums.UserRole.操作员, 2, cbg_Operator);
        }

        private async void btn_UpdateVisitorAuth_Click(object sender, EventArgs e)
        {
            await UpdateAuth(SystemEnums.UserRole.访客, 3, cbg_Visitor);
        }

        /// <summary>
        /// 更新角色权限
        /// </summary>
        /// <param name="role"></param>
        /// <param name="id"></param>
        /// <param name="cbg"></param>
        /// <returns></returns>
        private async Task UpdateAuth(SystemEnums.UserRole role, int id, UICheckBoxGroup cbg)
        {

            /* AuthUpdateDto update = new AuthUpdateDto() { Id = id, Role = role.ToString(), ControlModule = false, MonitorModule = false, RecipeModule = false, LogModule = false, ReportModule = false, ChartModule = false, ParamModule = false };
             foreach (var item in cbg.SelectedIndexes)
             {
                 switch (item)
                 {
                     case 0:
                         update.ControlModule = true;
                         break;
                     case 1:
                         update.MonitorModule = true;
                         break;
                     case 2:
                         update.RecipeModule = true;
                         break;
                     case 3:
                         update.LogModule = true;
                         break;
                     case 4:
                         update.ReportModule = true;
                         break;
                     case 5:
                         update.ChartModule = true;
                         break;
                     case 6:
                         update.ParamModule = true;
                         break;
                     default:
                         break;
                 }
             }*/

             List<int> authList = new List<int>();
            for(int i = 0; i < cbg.Controls.Count; i++)
            {
                if (cbg.SelectedIndexes.Contains(i))
                {
                    authList.Add(i);
                }
            }

            AuthUpdateDto update = new AuthUpdateDto() 
            { 
                Id = id, 
                Role = role.ToString(), 
                ControlModule = authList.Contains(0), 
                MonitorModule = authList.Contains(1), 
                RecipeModule = authList.Contains(2), 
                LogModule = authList.Contains(3),
                ReportModule = authList.Contains(4), 
                ChartModule = authList.Contains(5),
                ParamModule = authList.Contains(6) 
            };

            BaseResult res = await _authManager.UpdateAuthAsyncManager(update);
            if (res.ResultStatus == SystemEnums.ResultStatus.Success)
            {
                UIMessageTip.ShowOk("权限更新成功！");
            }
            else
            {
                UIMessageTip.ShowError("权限更新失败！");
            }
        }

        #region 窗体切换卡顿
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams paras = base.CreateParams;
                paras.ExStyle |= 0x02000000;
                return paras;
            }
        }
        #endregion
    }
}
